<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");

/**
 * Description of AddCpCom
 *
 * @author Administrator
 */
class AddCpCom {

    //put your code here
    function __construct() {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }

    function addCp($number, $totalfee, $createtime, $finaltime, $ids, $type, $info) {
        try {
            $sql = "insert into add_cp(number,total_fee,create_time,final_time,ids,type,info) values({$number},{$totalfee},'{$createtime}','{$finaltime}','{$ids}',{$type},'{$info}');SELECT @@Identity";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0]['@@Identity'];
            }
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }

    function getAllCp($minlimit, $count) {
        try {
            $sql = "select * from add_cp order by id desc limit {$minlimit},{$count}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return null;
    }

    function getNumber() {
        try {
            $sql = "select count(1) from add_cp";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            return false;
        }
        return 0;
    }

    function getCp($id) {
        try {
            $sql = "select * from add_cp where id={$id}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret [0][0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    
    function changeNumber($number,$id){
        try{
            $sql = "update add_cp set number={$number},ids='' where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    
}
